﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class UserController : BaseAdminController
    {
        private readonly IUserRepository _userRepository;

        public UserController(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(string oldPassword, string newPassword, FormCollection formCollection)
        {
            var model = _userRepository.Get().FirstOrDefault(o => o.Account == CurrentUserName && o.Password == oldPassword);
            if (model == null)
            {
                ViewData["Result"] = new AjaxResult { message = "老密码错误", state = true };
                return View();
            }
            model.Password = newPassword;
            _userRepository.Update(model);
            _userRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "修改成功", state = true };
            return View();
        }

    }
}
